package com.jz.banner.screen.banner.utils

import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import com.jz.banner.screen.ui.App

object GpsUtils {
    /**
     * 获取经纬度
     * 如果不支持GPS或GPS信号弱，使用网络获取
     *
     * @param context
     * @return
     */
    @SuppressLint("MissingPermission")  fun getLocationByGps(): String? {
        var latitude = 0.0
        var longitude = 0.0
        val locationManager = App.instance().getSystemService(Context.LOCATION_SERVICE) as LocationManager
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {  //从gps获取经纬度
            val location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
            if (location != null) {
                latitude = location.latitude
                longitude = location.longitude
            } else {
                //GPS信号弱，位置信息可能获取失败
                return getLocationByNet(App.instance())
            }
        } else {
            //网络获取经纬度
            return getLocationByNet(App.instance())
        }
        return "$longitude,$latitude"
    }

    //从网络获取经纬度
    @SuppressLint("MissingPermission") fun getLocationByNet(context: Context): String? {
        var latitude = 0.0
        var longitude = 0.0
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0f, listener)
        val location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
        if (location != null) {
            latitude = location.latitude
            longitude = location.longitude
        } else {
            return "error" //获取失败
        }
        val doubles = gcj02tobd09(longitude, latitude)
        println("谷歌经纬度：$longitude,$latitude")
        println("百度经纬度：" + doubles[0] + "," + doubles[1])
        return doubles[0].toString() + "," + doubles[1]
    }


    var listener: LocationListener = object : LocationListener {
        // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
        override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}

        // Provider被enable时触发此函数，比如GPS被打开
        override fun onProviderEnabled(provider: String) {}

        // Provider被disable时触发此函数，比如GPS被关闭
        override fun onProviderDisabled(provider: String) {}

        //当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
        override fun onLocationChanged(location: Location) {}
    }

    /*===========================坐标转换=========================*/

    /*===========================坐标转换=========================*/
    const val x_pi = 3.14159265358979324 * 3000.0 / 180.0

    /**
     * 火星坐标系(GCJ-02)转百度坐标系(BD-09)
     *
     *
     * 谷歌、高德——>百度
     *
     * @param lng 火星坐标经度
     * @param lat 火星坐标纬度
     * @return 百度坐标数组
     */
    fun gcj02tobd09(lng: Double, lat: Double): DoubleArray {
        val z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi)
        val theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi)
        val bd_lng = z * Math.cos(theta) + 0.0065
        val bd_lat = z * Math.sin(theta) + 0.006
        return doubleArrayOf(bd_lng, bd_lat)
    }

    /**
     * 百度坐标系(BD-09)转火星坐标系(GCJ-02)
     *
     *
     * 百度——>谷歌、高德
     *
     * @param bd_lon 百度坐标纬度
     * @param bd_lat 百度坐标经度
     * @return 火星坐标数组
     */
    fun bd09togcj02(bd_lon: Double, bd_lat: Double): DoubleArray? {
        val x = bd_lon - 0.0065
        val y = bd_lat - 0.006
        val z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
        val theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
        val gg_lng = z * Math.cos(theta)
        val gg_lat = z * Math.sin(theta)
        return doubleArrayOf(gg_lng, gg_lat)
    }
}